﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.UserControls
{
    public partial class EvaluationQuestionCategoryEvalMode : System.Web.UI.UserControl
    {
        public string CategoryName
        {
            get
            {
                return LabelCurrentCategory.Text;
            }
            set
            {
                LabelCurrentCategory.Text = value;
            }
        }

        private List<Evaluation_Question> savedQuestions;
        public List<Evaluation_Question> SavedQuestions
        {
            get
            {
                return savedQuestions;
            }
            set
            {
                savedQuestions = value;
            }
        }

        public int Min { get; set; }
        public int Max { get; set; }

        public bool DisabledValues = false;

        protected void Page_Load(object sender, EventArgs e)
        {

            CreateQuestions();
        }

        public void CreateQuestions()
        {
            Table t = new Table();
            t.ID = "TableQuestions";

            Label l = new Label();
            l.ID = "LabelHeader";
            l.Text = "Questions";

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Width = 800;
            tc.Controls.Add(l);

            tr.Cells.Add(tc);


            Label ln = new Label();
            ln.ID = "LabelNumber";// +j.ToString();
            for (int j = Min; j <= Max; ++j)
            {
                //if (j == Min)
                //{
                    //ln.Text += "<span style='padding-right:18px;padding-left:10px;'>" + j.ToString() + "</span>";
                ln.Text += "<span style='float:left;width:25px;text-align:center'>" + j.ToString() + "</span>";
                //}
                //else
                //{
                //    if (j == 9)
                //    {
                //        ln.Text += "<span style='padding-right:15px'>" + j.ToString() + "</span>";
                //    }
                //    else
                //    {
                //        ln.Text += "<span style='padding-right:20px'>" + j.ToString() + "</span>";
                //    }
                //}

            }
            TableCell tcln = new TableCell();
            tcln.Controls.Add(ln);
            tcln.HorizontalAlign = HorizontalAlign.Center;
            tr.Cells.Add(tcln);


            t.Rows.Add(tr);

            //currentNumberOfQuestions = (int)RadNumericTextBoxNumberOfQuestions.Value;

            for (int i = 0; i < savedQuestions.Count; ++i)
            {
                //RadTextBox q = new RadTextBox();
                Label lq = new Label();

               // q.ID = "Question" + i.ToString();
                lq.ID = "LabelQuestion" + i.ToString();
                //lq.Visible = true;
                lq.Text = savedQuestions[i].Question;
                
                //q.Width = 500;

                TableRow trQ = new TableRow();
                TableCell tcQ = new TableCell();
                //tcQ.Controls.Add(q);
                tcQ.Controls.Add(lq);

                trQ.Cells.Add(tcQ);

                //PanelQuestions.Controls.Add(q);

                //PanelQuestions.Controls.Add(new LiteralControl("<span>"));


                /*for (int j = Min; j <= Max; ++j)
                {
                    RadioButton rb = new RadioButton();
                    
                    string si = i < 10 ? "0" + i.ToString() : i.ToString();
                    string sj = j < 10 ? "0" + j.ToString() : j.ToString();

                    rb.ID = "Radio" + si + sj;
                    rb.GroupName = "Group" + i.ToString();
                    //rb.Enabled = false;
                    //PanelQuestions.Controls.Add(rb);

                    TableCell tcCH = new TableCell();
                    tcCH.Controls.Add(rb);

                    trQ.Cells.Add(tcCH);
                }*/

                RadioButtonList rbl = new RadioButtonList();
                rbl.ID = "rbl" + i.ToString();
                rbl.RepeatDirection = RepeatDirection.Horizontal;

                if (DisabledValues)
                {
                    rbl.Enabled = false;
                }

                for (int j = Min; j <= Max; ++j)
                {
                    ListItem li = new ListItem();
                    li.Value = j.ToString();
                    li.Text = "";
                    rbl.Items.Add(li);
                    
                }

                if (savedQuestions[i].Evaluation_User_Question_Value.Count != 0)
                {
                    rbl.SelectedValue = savedQuestions[i].Evaluation_User_Question_Value.FirstOrDefault().Value.ToString();
                }
                TableCell tcCH = new TableCell();
                tcCH.Controls.Add(rbl);

                trQ.Cells.Add(tcCH);

                t.Rows.Add(trQ);
            }

            PanelQuestions.Controls.Add(t);
        }

        public List<Evaluation_User_Question_Value> GetEvaluationValues()
        {
            List<Evaluation_User_Question_Value> result = new List<Evaluation_User_Question_Value>();

            Table t = (Table)this.FindControl("TableQuestions");

            for (int i = 0; i < savedQuestions.Count; ++i)
            {
                Evaluation_User_Question_Value euqv = new Evaluation_User_Question_Value();
                euqv.Evaluation_Question_FK = savedQuestions[i].ID;

                RadioButtonList rbl = (RadioButtonList)t.FindControl("rbl" + i.ToString());
                euqv.Value = Int32.Parse(rbl.SelectedValue);

                result.Add(euqv);
            }

            return result;
        }

        public void DisableValues()
        {
            Table t = (Table)this.FindControl("TableQuestions");

            foreach (Control c in t.Controls)
            {
                if (c.GetType().Name == "RadioButtonList")
                {
                    ((RadioButtonList)c).Enabled = false;
                }
            }
        }
    }
}    
    
